const User = require('../model/user.model')

class UserService {
    //创建用户
  async createUser(username, password) {
    // 插入数据
    // User.create({
    //   // 表的字段
    //   user_name: user_name,
    //   password: password
    // })

    // await表达式: promise对象的值
    const res = await User.create({ username:username, password:password })
    //console.log(res)

    return res.dataValues
  }
  //查询用户
  async getUerInfo({ id, username, password, }) {
    
    const whereOpt = {}

    id && Object.assign(whereOpt, { id })
    username && Object.assign(whereOpt, { username })
    password && Object.assign(whereOpt, { password })
    //console.log(whereOpt,'---');

    const res = await User.findOne({
      attributes: ['id', 'username', 'password'],
      where: whereOpt,
    })
    //console.log(res,'res');
    //console.log(res.dataValues,'----');

    return res ? res.dataValues : null
  }


}

module.exports = new UserService()